package com.example.demo.thread.thread;

/**
 * 线程的创建
 * @author: htc
 * @date: Created in 16:09 2019/1/2.
 */
public class TraditionalThread {
    public static void main(String[] args) {
//        demo1();
        System.out.println("##########################");
//        demo2();
        System.out.println("##########################");
        demo3();
    }

    /**
     * 创建线程方法一
     */
    public static void demo1() {
        //创建执行线索
        Thread thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " : is running ! " + System.currentTimeMillis());
//                    System.out.println(this.getName() + " : is running ! " + System.currentTimeMillis());
                }
            }
        };
        //运行线程
        thread.start();
    }

    /**
     * 创建线程方法二 (推荐：更加体现面向对象编程)
     */
    public static void demo2() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " : is running ! " + System.currentTimeMillis());
                }
            }
        });
        thread.start();
    }

    /**
     * 子类run方法与 Runnable的run方法
     */
    public static void demo3() {
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            try {
                                Thread.sleep(300);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + " : is running  Runnale code ! " + System.currentTimeMillis());
                        }
                    }
                }
        ) {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " : is running subclass code ! " + System.currentTimeMillis());
                }
            }
        }.start();
    }
}
